//
//  ThirdViewController.m
//  mac2
//
//  Created by 李劭华 on 2021/7/26.
//
#import <Masonry/Masonry.h>

#import "ThirdViewController.h"
#define MyTableCellViewDataType @"MyTableCellViewDataType"

@interface ThirdViewController ()<NSTableViewDelegate,NSTableViewDataSource>
@property (nonatomic,strong) NSButton *firstBtn;
@property (nonatomic,strong) NSScrollView *scrollView;
@property (nonatomic,strong) NSTableView *tableView;
@property (nonatomic,strong) NSMutableArray *datArray;


@end

@implementation ThirdViewController

- (BOOL)isFlipped{
    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    
    self.view.wantsLayer = YES;
    self.view.layer = [CALayer layer];
    self.view.layer.backgroundColor = [NSColor whiteColor].CGColor;
    self.view.frame = self.rect;
    [self initDataSource];
    [self initUI];
}
-(void)initDataSource{
    self.datArray=[NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i<20; i++) {
        [self.datArray addObject:[NSString stringWithFormat:@"当前数据----：%d",i]];
    }
    
}
-(void)initUI{
    [self.view addSubview:self.firstBtn];
    [self.firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(0);
        make.left.equalTo(self.view.mas_left).with.offset(15);
        make.width.height.mas_equalTo(100);
    }];
    
    [self.view addSubview:self.scrollView];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.firstBtn.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    self.scrollView.contentView.documentView = self.tableView;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView.contentView);
        make.height.mas_equalTo(1000);
    }];
}
#pragma mark - 点击事件
-(void)touchBtn:(NSButton *)sender{
    NSLog(@"touchBtn---:%ld",(long)sender.tag);
    [self dismissViewController:self];

   
    
}
#pragma mark - 代理事件

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
    return self.datArray.count;
}

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    return self.datArray[row];
}

-(void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSTextField *_cell = cell;
    _cell.textColor = [NSColor redColor];
}
-(NSString *)tableView:(NSTableView *)tableView toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row mouseLocation:(NSPoint)mouseLocation
{
    if (row<self.datArray.count) {
        return self.datArray[row];
    }
    return @"";
}
-(BOOL)tableView:(NSTableView *)tableView shouldShowCellExpansionForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    return YES;
}
-(CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row{
    return 40;
}
//排序回调函数
-(void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray<NSSortDescriptor *> *)oldDescriptors{
    NSLog(@"排序回调函数%@",oldDescriptors[0]);
}

// drag operation stuff
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
    // Copy the row numbers to the pasteboard.
//    NSData *zNSIndexSetData = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
    NSData *zNSIndexSetData =  [NSKeyedArchiver archivedDataWithRootObject:rowIndexes requiringSecureCoding:YES error:nil];
    [pboard declareTypes:[NSArray arrayWithObject:MyTableCellViewDataType] owner:self];
    [pboard setData:zNSIndexSetData forType:MyTableCellViewDataType];
    return YES;
}

- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)op
{
    // Add code here to validate the drop
    //NSLog(@"validate Drop");

    return NSDragOperationEvery;
}

- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info
      row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
    NSPasteboard* pboard = [info draggingPasteboard];
    NSData* rowData = [pboard dataForType:MyTableCellViewDataType];
//    NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
    NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSIndexSet class] fromData:rowData error:nil];
    NSInteger dragRow = [rowIndexes firstIndex];

        // Move the specified row to its new location...
        // if we remove a row then everything moves down by one
        // so do an insert prior to the delete
        // --- depends which way we're moving the data!!!
    if (dragRow < row) {
        [self.datArray insertObject:[self.datArray objectAtIndex:dragRow] atIndex:row];
        [self.datArray  removeObjectAtIndex:dragRow];
        [self.tableView noteNumberOfRowsChanged];
        [self.tableView reloadData];
        
        return YES;
        
    } // end if

    NSString * zData = [self.datArray objectAtIndex:dragRow];
    [self.datArray removeObjectAtIndex:dragRow];
    [self.datArray insertObject:zData atIndex:row];
    [self.tableView noteNumberOfRowsChanged];
    [self.tableView reloadData];

    return YES;
}

#pragma mark - ui
-(NSButton *)firstBtn
{
    if (!_firstBtn) {
        _firstBtn = [NSButton buttonWithTitle:@"返回" target:self action:@selector(touchBtn:)];
//        _firstBtn.frame = CGRectMake(10, 20, 100, 40);
        _firstBtn.tag = 100;
    }
    return _firstBtn;
}
-(NSTableView *)tableView
{
    if (!_tableView) {
        _tableView = [[NSTableView alloc]initWithFrame:CGRectZero];
        NSTableColumn *column1 =[[NSTableColumn alloc]initWithIdentifier:@"column1_Identifier"];
        
        [_tableView addTableColumn:column1];
        
        NSTableColumn *column2 =[[NSTableColumn alloc]initWithIdentifier:@"column2_Identifier"];
        [_tableView addTableColumn:column2];
        
        NSTableColumn *column3 =[[NSTableColumn alloc]initWithIdentifier:@"column3_Identifier"];
        [_tableView addTableColumn:column3];

        NSTableColumn *column4 =[[NSTableColumn alloc]initWithIdentifier:@"column4_Identifier"];
        [_tableView addTableColumn:column4];

        NSTableColumn *column5 =[[NSTableColumn alloc]initWithIdentifier:@"column5_Identifier"];
        [_tableView addTableColumn:column5];

        NSTableColumn *column6 =[[NSTableColumn alloc]initWithIdentifier:@"column6_Identifier"];
        [_tableView addTableColumn:column6];

        _tableView.delegate = self;
        _tableView.dataSource= self;
        
        [_tableView.tableColumns enumerateObjectsUsingBlock:^(NSTableColumn * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.minWidth = 150;
            obj.title = [NSString stringWithFormat:@"第%lu列",(unsigned long)idx];
            obj.headerToolTip = [NSString stringWithFormat:@"鼠标放在第%lu列",(unsigned long)idx];
            obj.sortDescriptorPrototype = [NSSortDescriptor sortDescriptorWithKey:obj.title ascending:NO];
        }];
        [_tableView registerForDraggedTypes:[NSArray arrayWithObject:MyTableCellViewDataType] ];

    }
    return _tableView;
}

-(NSScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[NSScrollView alloc]initWithFrame:CGRectZero];
        _scrollView.hasVerticalScroller = YES;
        _scrollView.hasHorizontalScroller = YES;
    }
    return _scrollView;
}

@end
